/*
  SDL Programming example for PSP

  by Int-0 (CRySoL)

  22 jul 2007
*/


#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <errno.h>
#include <unistd.h>

#include "libmpdclient.h"
#include "util.h"
#include "sdl.h"
#include "loadscreen.h"
#include "menu.h"
#include "menuFiles.h"
#include "menuPlaylist.h"
#include "errorscreen.h"

#ifdef PSP
#include "psp.h"
#define EVENT_TYPE SDL_JOYBUTTONDOWN

SDL_Joystick *stick = NULL;
#else
#define EVENT_TYPE SDL_KEYDOWN
#endif





void fatalError(char * str, ...)
{
    fprintf(stderr, "Error: %s: %s\n", str, SDL_GetError());
    SDL_Quit();
#ifdef PSP
    clean_psp();
    SDL_JoystickClose( stick );
#endif
    exit(1);
}

void main_loop()
{
    mpd_Status * status;
    mpd_Connection * con;
    int current = 0;
    SDL_Event event;
    menu * menu[2];
    int done = 0;

    //loading_screen_paint("Connection to 192.168.1.130:6600");
    con = mpd_newConnection("192.168.1.130", 6600, 1000);
    if (con)
    {

        menu[0] = createPlaylistMenu();
        menu[0]->init(menu[0]->state, con);
        menu[0]->aboutToShow(menu[0]->state, con);
        //printf("Creado menu 1");
        menu[1] = createFilesMenu();
        menu[1]->init(menu[1]->state,con);

        int menus = 2;
        while (!done)
        {
            #ifdef PSP
            if(!check_connection_to_apctl())
            {
                //mpd_closeConnection(con);
                con = mpd_newConnection("192.168.1.130", 6600, 1000);
                if(!con)
                {
                    app_error("Unable to connect!");
                }
                menu[current]->aboutToShow(menu[current]->state,con);
            }
            #endif
            // message processing loop
            while (SDL_PollEvent(&event))
            {
                switch (event.type)
                {
                    // exit if the window is closed
                case SDL_QUIT:
                    done = 1;
                    break;

                case EVENT_TYPE:
                {
                    #ifdef PSP
                    switch (event.jbutton.button)
                    #else
                    switch (event.key.keysym.sym)
                    #endif
                    {
                    case BUTTON_RT:
                        menu[current]->handleRT(menu[current]->state, con);
                        break;
                    case BUTTON_LT:
                        menu[current]->handleLT(menu[current]->state, con);
                        break;
                    case BUTTON_UP:
                        menu[current]->handleUp(menu[current]->state, con);
                        break;
                    case BUTTON_DOWN:
                        menu[current]->handleDown(menu[current]->state, con);
                        break;
                    case BUTTON_O:
                        menu[current]->handleC(menu[current]->state, con);
                        break;
                    case BUTTON_X:
                        menu[current]->handleX(menu[current]->state, con);
                        break;
                    case BUTTON_S:
                        menu[current]->handleS(menu[current]->state, con);
                        break;
                    case BUTTON_T:
                        menu[current]->handleT(menu[current]->state, con);
                        break;
                    case BUTTON_LEFT:
                        if (current != 0)
                        {
                            menu[current]->aboutToHide(
                                menu[current]->state, con);
                            current--;
                            menu[current]->aboutToShow(
                                menu[current]->state, con);

                        }
                        break;
                    case BUTTON_RIGHT:
                        if (current != menus - 1)
                        {
                            menu[current]->aboutToHide(
                                menu[current]->state, con);
                            current++;
                            menu[current]->aboutToShow(
                                menu[current]->state, con);

                        }
                        break;
                    case BUTTON_START:
                        mpd_sendStatusCommand(con);
                        status = mpd_getStatus(con);
                        mpd_finishCommand(con);
                        if (status)
                        {
                            if (status->state == MPD_STATUS_STATE_PLAY)
                            {
                                mpd_sendPauseCommand(con, 1);
                                mpd_finishCommand(con);
                            }
                            else if (status->state
                                     == MPD_STATUS_STATE_PAUSE)
                            {
                                mpd_sendPauseCommand(con, 0);
                                mpd_finishCommand(con);
                            }
                            else if (status->state
                                     == MPD_STATUS_STATE_STOP)
                            {
                                mpd_sendPlayCommand(con, 0);
                                mpd_finishCommand(con);
                            }
                            mpd_freeStatus(status);
                        }
                        break;
                    case BUTTON_SELECT:
                        mpd_sendStatusCommand(con);
                        mpd_Status * status = mpd_getStatus(con);
                        mpd_finishCommand(con);
                        if (status)
                        {
                            int random = status->random;
                            mpd_freeStatus(status);
                            mpd_sendRandomCommand(con, !random);
                            mpd_finishCommand(con);
                        }
                        break;


                    default:
                        break;
                    }



                }
                }
            }
#ifdef PSP
            sceKernelDelayThread(50 * 1000);
#else
	    SDL_Delay(10);
#endif
        }


    }
    else
    {
        app_error("Failed to connect!");
    }


}

void cleanup()
{
    loading_screen_close();
    SDL_Quit();
#ifdef PSP
    clean_psp();
#endif

}

int main(int argc, char *argv[])
{

    atexit(cleanup);
    /* Inicializamos SDL y cargamos los gráficos */
#ifdef PSP
    init_psp();
#endif
    init_sdl();
    //loading_screen_init();
    //loading_screen_paint("Initializing...");

#ifdef PSP
    //if (connect_to_apctl(1))
    //{
        stick = SDL_JoystickOpen( 0 );
        check_connection_to_apctl();
        //loading_screen_close();
        //main_loop();
   // }
//#else
    //loading_screen_close();
    
#endif
    main_loop();
    printf("Adios!");	
    exit(0);
}
